﻿--[[
   @brief    ui logic for login ui
   @author   rickonzhang
   @version  1.0
   @date     2015-03-07
--]]

EUpdateRes = 
{
};

update_dialog_control =
{
    sNewVer = nil;
    advice_update_flag = false;

    local_record_key = "version_info.show_tips_version";
    has_show_tips_version_list = {};
    need_show_version_tips = false;

    version_tips_cfg = {};
    is_req_version_tips_cfg = false;

    --升级信息跳转url
    update_url   = LoadStr("WEGAME_UPDATE_URL");
    histrory_url = LoadStr("WEGAME_UPDATE_URL");

    inner_log = function (msg)
        base_utility.log(string.format("[main_menue_update] %s", msg), true);
    end;
    
    init = function()
        base_utility.log("update_dialog_control.init called");
        ierd_tgp.tpf_ui.Require("main_menue_update\\view.lua");
    end;
    
    open_update_dialog = function(dwEventId, json_str)
        ierd_tgp.tpf_ui.Log("update_dialog_control recve event");
        ierd_tgp.tpf_ui.Require("main_menue_update\\view.lua");
        update_dialog_view.show(update_dialog_control.advice_update_flag);

        update_dialog_control.query_version_info();
    end;

    query_version_info = function ()
         -- 请求版本说明
        if update_dialog_control.is_req_version_tips_cfg == false then
            update_dialog_control.version_tips_cfg = {};
            update_dialog_control.req_version_tips_config(0);
        end;
    end;

    req_version_tips_config = function(start_index)
        update_dialog_control.is_req_version_tips_cfg = true;
        if nil == start_index then
            start_index = 0;
        end
    
        local client_type = 1;
        if base_utility.is_type_of_oversea_ver() then
            client_type = 2;
        end

        local post_data = {
                data_names = "wegame_home_changelogs",
                command = "list_all",
                params = {
                    start_page = start_index,
                    items_per_pager = 50
                },
                stamp = {
                    credential = {
                        agent_client_type = client_type,
                    },
                    agent_client_language = base_utility.get_currrent_language_str();
                }
            };
        base_utility.curl_request_async(LoadStr("GL_GAME_CONFIG_URL")
        , "update_dialog_control.on_req_version_tips_config"
        , false, nil, true, nil, base_utility.table_to_json(post_data));
    end;

    on_req_version_tips_config = function(response)
        update_dialog_control.is_req_version_tips_cfg = false;
        local res_tbl = base_utility.json_to_table(response);
        if nil == res_tbl or nil == res_tbl.result then
            return;
        end
        if 0 ~= tonumber(res_tbl.result.error_code) then
            return;
        end

        local items_count = 0;
        if nil ~= res_tbl.items and type(res_tbl.items) == "table" then
            items_count = #res_tbl.items;
            for k, v in pairs(res_tbl.items) do
                update_dialog_control.version_tips_cfg[k] = v;
            end
        end

        -- request more
        local total_items = tonumber(res_tbl.total_items or "0");
        local start_page = tonumber(res_tbl.start_page or "0");
        local items_per_page = tonumber(res_tbl.items_per_pager or "0");

        if total_items > (start_page * items_per_page + items_count) then
            update_dialog_control.req_version_tips_config(start_page + 1);
        else
            update_dialog_control.on_version_update_tips_rsp(update_dialog_control.version_tips_cfg);
        end;
    end;

    -- 获取到配置
    on_version_update_tips_rsp = function(rsp_data)
        if nil == rsp_data or rsp_data == "" then
            return;
        end;

        local sTemp = "";
        if (nil ~= update_dialog_control.sNewVer) then
            sTemp = update_dialog_control.sNewVer;
        else
            local sTGPVer = NewStr(260);
            local pGMLib  = base_utility.get_component_adapter("IGame_library",
                            "ierd_tgp.game_library.Get_game_library_adapter();");
            local bDevVer = pGMLib:get_tgp_version(sTGPVer, 260, false);

            sTemp = CUIStrtoU8(U8toCUIStr(sTGPVer));
        end;

        local version_info = "";
        for nIdx, nVal in pairs(rsp_data) do
            local item = rsp_data[nIdx];
            local zContent = item["content"];
            local tips_info = base_utility.replace_plain(zContent, "\n", "<b>");
            if (sTemp == item["version"]) then
                -- 更新跳转链接
                if item["version_url"] ~= nil and item["version_url"] ~= "" then
                    update_dialog_control.histrory_url = item["version_url"];
                end;
                -- 更新版本更新内容
                update_dialog_view.set_version_desp(tips_info, sTemp);
                version_info = {ver = sTemp, 
                                ver_info = tips_info,
                                ver_title = item["title"],
                                ver_url = item["version_url"],
                                ver_tips_show = item["version_tips_show"]};
                comm_center.send_broadcast("i_broadcast_update_version_info", version_info);
                break;
            end;
        end;

        update_dialog_control.check_show_version_tips(version_info);
    end;

    --升级检查完成
    on_preupdate_detected = function(dwEventId, json_str)
        ierd_tgp.tpf_ui.Log("[update_dialog_control] recv kLiveupdateCheckDone cevent");
        local param_table = base_utility.json_to_table(json_str);
        if param_table then
            if param_table["has_recommend_update"] == true then
                ierd_tgp.tpf_ui.Log("[update_dialog_control] param_table.hasRecommend:1");
            else
                ierd_tgp.tpf_ui.Log("[update_dialog_control] param_table.hasRecommend:0");
            end;
            update_dialog_control.advice_update_flag = param_table["has_recommend_update"];

            update_dialog_control.sNewVer = param_table["ver"];
        end;
    end;
    
    --通知c++进行升级
    do_update_app = function()
        local gamelib =
      base_utility.get_component_adapter("IGame_library",
          "ierd_tgp.game_library.Get_game_library_adapter();");
        gamelib:do_update_app();
    end;
    
    go_to_update_web = function()
        ierd_tgp.tpf_ui.Log("[update_dialog_control] web to update url:"..update_dialog_control.update_url);
        web_utility.open_browser(update_dialog_control.update_url, false, true);
    end;
    
    go_to_history_web = function()
        ierd_tgp.tpf_ui.Log("[update_dialog_control] web to history url:"..update_dialog_control.histrory_url);
        web_utility.open_browser(update_dialog_control.histrory_url, false, true);
    end;

    query_update_version_info = function ()
        update_dialog_control.query_version_info();
    end;

    on_main_wnd_show = function(param_tbl)
        if param_tbl == nil or param_tbl.isfirst == nil or param_tbl.isfirst ~= 1 then
            return;
        end;

        local zParam = {};
        local info = comm_center.call_service("Srv_IsRestartAfterUpdate", zParam);
        if info == nil or 
           info.is_update_restart == nil or info.is_update_restart == 0 or
           base_utility.is_netbar_ver() == true then
            update_dialog_control.inner_log("no need show tips, netbar or not update restart");
            return;
        end;

        update_dialog_control.read_local_record();
        if update_dialog_control.has_show_version_tips() == false then
            update_dialog_control.need_show_version_tips = true;
            update_dialog_control.query_version_info();
        end;
    end;
    
    -- 读取本地记录
    read_local_record = function()
        local data = {};
        data["common"] = 1;
        data["key"] = update_dialog_control.local_record_key;
        local info = comm_center.call_service("Svr_GetConfig", data);
        if info ~= nil and info ~= "" and info.value ~= nil and info.value ~= "" then
            local val_tbl = base_utility.json_to_table(info.value);
            if val_tbl ~= nil and val_tbl ~= "" then
                update_dialog_control.has_show_tips_version_list = val_tbl;
            end;
        end;
    end;

    -- 是否已经显示过，升级后的版本更新提示
    has_show_version_tips = function ()
        local sTGPVer = NewStr(260);
        local pGMLib  = base_utility.get_component_adapter("IGame_library",
                            "ierd_tgp.game_library.Get_game_library_adapter();");
        local bDevVer = pGMLib:get_tgp_version(sTGPVer, 260, false);
        local localVer = CUIStrtoU8(U8toCUIStr(sTGPVer));

        if localVer == nil or localVer == "" then
            return true;
        end;
        update_dialog_control.inner_log(
           "record:"..base_utility.table_to_json(update_dialog_control.has_show_tips_version_list));
        local has_show = false;
        for key, value in pairs(update_dialog_control.has_show_tips_version_list) do
            if localVer == value then
                has_show = true;
                break;
            end;
        end;
        return has_show;
    end;

    check_show_version_tips = function(info)
        if update_dialog_control.need_show_version_tips then

            if info ~= nil then
                update_dialog_control.inner_log(
                   "check_show_version_tips : "..base_utility.table_to_json(info));
            end;

            update_dialog_control.need_show_version_tips = false;
            if info ~= nil and info ~= "" and 
               info.ver ~= nil and info.ver ~= "" and
               info.ver_tips_show ~= nil and info.ver_tips_show == "1" then
                update_dialog_control.add_to_show_tips_version_list(info.ver);
                --update_dialog_view.show(false);
                update_dialog_view.set_version_desp(info.ver_info, info.ver);
                update_dialog_control.show_version_update_tips(info);
            end;
        end;
    end;

    add_to_show_tips_version_list = function(version)
        if version == nil or version == "" then
            return;
        end;

        table.insert(update_dialog_control.has_show_tips_version_list, version);
        local data = {};
        data["common"] = 1;
        data["key"] = update_dialog_control.local_record_key;
        data["value"] = base_utility.table_to_json(update_dialog_control.has_show_tips_version_list);
        local config = comm_center.call_service("Svr_SetConfig", data);
        return config;
    end;

    show_version_update_tips = function(info)
        update_dialog_control.inner_log("show_version_update_tips");
        local tip_data = {};
        tip_data.id = os.time();
        tip_data.tips_main_type = tip_type_constant.MAIN_TYPE_NORMAL;
        tip_data.tips_sub_type = tip_type_constant.sub_type_update_result;

        -- tips标题
        tip_data.tips_title = LoadStr("WEGAME_SYS_MENU_VERSION_TIPS_TITLE");

        -- tips主体内容
        tip_data.main_title = info.ver_title;
        tip_data.sub_title = string.format(LoadStr("WEGAME_SYS_MENU_VERSION_TIPS_SUB_TITLE"), tostring(info.ver));
        tip_data.show_duration = 10 * 1000;

        tip_data.tips_ok_btn = LoadStr("WEGAME_SYS_MENU_VERSION_TIPS_MORE");
        tip_data.ok_service = "i_broadcast_show_update_dlg";

        comm_center.send_broadcast("i_broadcast_tips_show_com", tip_data);
        tips_reveive_common.tips_qos("wg_common_tip", "version_update_tips", tip_data);
    end;
};

update_dialog_control.init();